<?php
namespace App\Http\Controllers\ShopApi\V1\SalesmanManager;

use App\Http\Controllers\ShopApiController;
use App\Http\Controllers\Business\Shop\V1\Salesman\SalesmanManagement;
use App\Http\Controllers\Business\Shop\V1\OrderManagement\OrderManagement;

class SalesmanManagerController extends ShopApiController
{
    //首页的接口
    public function index() {
        //该接口的数据已经放到首页了
//        return;
        $shopUserId = $this->shopUserId;
        $shopId = $this->request->get('shopid');
        $result = SalesmanManagement::index($shopUserId, $shopId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            $data = $result['data'];
            return $this->responseSuccessData($data);
        }
    }
    //注册用户
    public function registerUserList() {
        $shopUserId = $this->shopUserId;
        $shopId = $this->request->get('shopid');
        if(($this->request->has('source')) && ($this->request->get('source') != '')) {
            $source = $this->request->get('source');
        } else {
            $source = $shopUserId; //默认是本人的手机id
        }
        if($this->request->has('starttime')) {
            $startTime = $this->request->get('starttime');
            $lastTime = $this->request->get('lasttime');
        } else {
            $startTime = date('Y-m-d', strtotime("-7days"));
            $lastTime = date('Y-m-d');
        }
        if($this->request->has('page')) {
            $page = $this->request->get('page');
        } else {
            $page = 1;
        }
        $result = SalesmanManagement::registerList($shopId, $shopUserId, $source,$page, $startTime, $lastTime);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        return $this->responseSuccessData($result['data']);
    }
    //有效订单
    public function validOrderList() {
        $shopId = $this->request->get('shopid');
        if($this->request->has('starttime')) {
            $startTime = $this->request->get('starttime');
            $lastTime = $this->request->get('lasttime');
        } else {
            $startTime = date('Y-m-d', strtotime("-7days"));
            $lastTime = date('Y-m-d');
        }
        if($this->request->has('page')) {
            $page = $this->request->get('page');
        } else {
            $page = 1;
        }
        if(!$this->request->has('apiversion')) {
            $result = SalesmanManagement::validOrderList($shopId, $page, $startTime, $lastTime);
        } else {
            $apiVersion = $this->request->get('apiversion');
            if($apiVersion == 'v2') {
                $result = SalesmanManagement::validOrderListByV2Version($shopId, $page, $startTime, $lastTime);
            } else {
                return $this->responseError('版本号错误');
            }
        }
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        return $this->responseSuccessData($result['data']);
    }
}